package com.concurrent.coline.part2.syn;

/**
 * 类锁
 */
public class ClassLockSyn {
    private static int total = 10;

    // 类锁方式一
    public static void classLockMethod(){
        int num = 5;
        while (num > 0){
            --num;
            --total;
            System.out.println("Thread name: "+Thread.currentThread().getName()+"---total: "+total);
        }
    }

    // 类锁方式二
    public void classLockMethod1(){
        synchronized (ClassLockSyn.class){
            int num = 5;
            while (num > 0){
                --num;
                --total;
                System.out.println("Thread name: "+Thread.currentThread().getName()+"---total: "+total);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ClassLockSyn classLockSyn = new ClassLockSyn();
        new Thread(() ->{
            ClassLockSyn.classLockMethod();
        }).start();

        new Thread(()->{
            classLockSyn.classLockMethod1();
        }).start();
    }
}
